<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Customer;

/**
 * CustomerSearch represents the model behind the search form about `app\models\Customer`.
 */
class CustomerSearch extends Customer
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['cust_id', 'close_fee', 'close_month', 'close_compensation_month', 'payroll_compensation', 'payroll_month', 'payroll_compensation_month'], 'integer'],
            [['cust_name', 'address', 'person_in_charge', 'email', 'tel', 'ns_person', 'cust_type', 'declaration_form', 'notification_form', 'remark'], 'safe'],
            [['is_operating', 'is_tax_form', 'is_bookkeeping', 'is_labor_form', 'is_year_end_adjustment', 'is_legal_adjustment'], 'boolean'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Customer::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'cust_id' => $this->cust_id,
            'close_fee' => $this->close_fee,
            'close_month' => $this->close_month,
            'is_operating' => $this->is_operating,
            'close_compensation_month' => $this->close_compensation_month,
            'payroll_compensation' => $this->payroll_compensation,
            'payroll_month' => $this->payroll_month,
            'payroll_compensation_month' => $this->payroll_compensation_month,
            'is_tax_form' => $this->is_tax_form,
            'is_bookkeeping' => $this->is_bookkeeping,
            'is_labor_form' => $this->is_labor_form,
            'is_year_end_adjustment' => $this->is_year_end_adjustment,
            'is_legal_adjustment' => $this->is_legal_adjustment,
        ]);

        $query->andFilterWhere(['like', 'cust_name', $this->cust_name])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'person_in_charge', $this->person_in_charge])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'tel', $this->tel])
            ->andFilterWhere(['like', 'ns_person', $this->ns_person])
            ->andFilterWhere(['like', 'cust_type', $this->cust_type])
            ->andFilterWhere(['like', 'declaration_form', $this->declaration_form])
            ->andFilterWhere(['like', 'notification_form', $this->notification_form])
            ->andFilterWhere(['like', 'remark', $this->remark]);

        return $dataProvider;
    }
}
